package org.jeecg.modules.live.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.config.LiveConfig;
import org.jeecg.modules.live.entity.LiveApply;
import org.jeecg.modules.live.entity.LiveInfo;
import org.jeecg.modules.live.service.ILiveInfoService;
import org.jeecg.util.CommonUtil;
import org.jeecg.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
* @Description: 直播信息
* @Author: jeecg-boot
* @Date:   2020-05-06
* @Version: V1.0
*/
@Slf4j
@Api(tags="直播信息")
@RestController
@RequestMapping("/live/liveInfo")
public class LiveInfoController extends JeecgController<LiveInfo, ILiveInfoService> {
   @Autowired
   private ILiveInfoService liveInfoService;

    @Autowired
    private LiveConfig liveConfig;
   /**
    * 分页列表查询
    *
    * @param liveInfo
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "直播信息-分页列表查询")
   @ApiOperation(value="直播信息-分页列表查询", notes="直播信息-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(LiveInfo liveInfo,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<LiveInfo> queryWrapper = QueryGenerator.initQueryWrapper(liveInfo, req.getParameterMap());
       Page<LiveInfo> page = new Page<LiveInfo>(pageNo, pageSize);
       IPage<LiveInfo> pageList = liveInfoService.page(page, queryWrapper);
       return Result.ok(pageList);
   }

   /**
    * 添加
    *
    * @param liveInfo
    * @return
    */
   @AutoLog(value = "直播信息-添加")
   @ApiOperation(value="直播信息-添加", notes="直播信息-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody LiveInfo liveInfo) {
       liveInfoService.save(liveInfo);
       return Result.ok("添加成功！");
   }

   /**
    * 编辑
    *
    * @param liveInfo
    * @return
    */
   @AutoLog(value = "直播信息-编辑")
   @ApiOperation(value="直播信息-编辑", notes="直播信息-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody LiveInfo liveInfo) {
       liveInfoService.updateById(liveInfo);
       return Result.ok("编辑成功!");
   }



    /**
     * 手动添加直播信息
     *
     * @param liveInfo
     * @return
     */
    @AutoLog(value = "直播信息 手动添加直播信息")
    @ApiOperation(value="直播信息 手动添加直播信息", notes="直播信息 手动添加直播信息")
    @PostMapping(value = "/addManuLive")
    public Result<?> addManuLive(@RequestBody LiveInfo liveInfo) {

        String pushUrl;// 直播推流地址
        String broadcastUrl;// 直播播放地址
        String streamId; // 流id
        String authorityKey; // 鉴权 Key
            String txTime ="73c5250c";
            int ran2 = (int) (Math.random()*1000);
            streamId = UUIDGenerator.generate() +"A"+ran2;
            String txSecret = CommonUtil.getSafeUrl(liveConfig.getPushKey(),streamId,txTime);
            pushUrl = "rtmp://"+
                    liveConfig.getPushDomainName()+"/"+
                    liveConfig.getAppName()+"/"+
                    streamId+"?"+txSecret;
            broadcastUrl = "http://"+
                    liveConfig.getBroadcastDomainName()+"/"+
                    liveConfig.getAppName()+"/"+
                    streamId+".flv?"+txSecret;


//		 liveInfo.setCreateBy(liveApply.getCreateBy());
        liveInfo.setCreateTime(DateUtils.getDate());
        long nowTime = DateUtils.getDate().getTime();

            liveInfo.setLiveStatus(0);

        liveInfo.setPushUrl(pushUrl);
        liveInfo.setBroadcastUrl(broadcastUrl);
        liveInfo.setStreamId(streamId);

        return Result.OK(broadcastUrl);
    }


}
